package com.example.forum.services.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * date: 2024/09/11 10:10<br/>
 *
 * @author: jmy <br/>
 */

@Service
@Slf4j
public class BoardServicesImpl implements IBoardServices {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        if (num == null || num <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        //
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    @Override
    public Integer addOneArticleCountById(Long id) {
        if (id == null || id <= 0) {
            log.error(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS + "board:" + id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //跟新帖子数量
        return boardMapper.addBoardCountById(board.getId());
    }

    @Override
    public Integer subOneArticleCountById(Long id) {
        //非空校验
        if (id == null || id <= 0) {
            log.error(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS + "board:" + id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        //如果board 帖子数本来就为 0 则直接返回
        if (board.getArticleCount() == 0) {
            return 1;
        }
        //跟新帖子数量 -1
        return boardMapper.subBoardCountById(board.getId());
    }

    @Override
    public Board selectByPrimaryKey(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE + "board: " + id);
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateByPrimaryKeySelective(Board board) {
        if (board == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        return boardMapper.updateByPrimaryKeySelective(board);
    }

}
